/*	******************************************
	File:		StateGame.cpp
	Author:		Adnan Srna, Peter Kober, Ivo Havranek
	Purpose:	AugmentedReality 2
	(up)Date:	March/April 2010
	Descr.:		Game State, demo game for 
				augmented presentation
	Origin:		Salzburg University of Applied Science
	******************************************	*/

#include "StateSoundTest.h"
#include "StateVideoTest.h"
#include "StateGame.h"
#include "StateGameOver.h"


StateGame::StateGame(){
	ofAddListener(ofEvents.keyPressed, this, &StateGame::keyPressed);
}

StateGame::~StateGame(){
	ofRemoveListener(ofEvents.keyPressed, this, &StateGame::keyPressed);
}

void StateGame::setup(StateManager* _manager){
	manager = _manager;
	gameTool.setup();
}

// now threadsafe thanks to Robert Praxmarer :D
void StateGame::update(){
	gameTool.update();
	if(gameTool.lives <= 0){
		gameTool.threadFace.lock();
			gameTool.threadFace.stopThread();
		gameTool.threadFace.unlock();

		changeState(manager, new StateGameOver());
	}
}

void StateGame::draw(){
	gameTool.draw();
}

void StateGame::keyPressed(ofKeyEventArgs& arg)
{
	switch(arg.key){
		case '1':
			gameTool.threadFace.lock();
				gameTool.threadFace.stopThread();
			gameTool.threadFace.unlock();

			changeState(manager, new StateSoundTest());
			break;
		case '2':
			gameTool.threadFace.lock();
				gameTool.threadFace.stopThread();
			gameTool.threadFace.unlock();

			changeState(manager, new StateVideoTest());
			break;
		case 'q':
			ofAppGlutWindow::exitApp();
			break;
	}
}
